﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Drawing.Drawing2D;

namespace Grizzy_X
{
    public class CText
    {
        private int ID;
        public struct SimpleTextBox
        {
            public PointF lupt;
            public PointF lbpt;
            public PointF rupt;
            public PointF rbpt;
        }
        public List<SimpleTextBox> textboxes;
        public List<PointF> points;
        public List<String> texts;
        public List<SizeF> sizes;
        public MBR MinBR;
        public int LegendID;
        public bool isselected;
        public Font textfont;
        public SolidBrush textbrush;
       
        public int GetID()
        {
            return ID;
        }

        public void newCtext(int txid,int legdid)
        {
            ID = txid;
            LegendID = legdid;
            textboxes = new List<SimpleTextBox>();
            points = new List<PointF>();
            texts = new List<string>();
            sizes=new List<SizeF>();
            MinBR = new MBR();
            isselected = false;
        }

        public void addpoint_text(PointF textp, string t)
        {
            points.Add(textp);
            texts.Add(t);
            float trans;
            if (Controller.MyMap.Unit == "英寸")
                trans = (float)1 / 96;
            else
                trans = (float)2.54 / 96;
            Bitmap tx = new Bitmap(50, 50);
            Graphics a = Graphics.FromImage(tx);
            CTextLegend ctl = (CTextLegend)Controller.MapLegends[LegendID];
            textfont = ctl.font;
            SizeF textsize = a.MeasureString(t, textfont);
            SizeF realsize = new SizeF(textsize.Width * trans, textsize.Height * trans);
            sizes.Add(realsize);
            SimpleTextBox stb = new SimpleTextBox();
            stb.lupt = new PointF(textp.X - realsize.Width / 2, textp.Y + realsize.Height / 2);
            stb.lbpt = new PointF(textp.X - realsize.Width / 2, textp.Y - realsize.Height / 2);
            stb.rupt = new PointF(textp.X +realsize.Width / 2, textp.Y + realsize.Height / 2);
            stb.rbpt = new PointF(textp.X + realsize.Width / 2, textp.Y - realsize.Height / 2);
            textboxes.Add(stb);
        }
        public void updateMBR()
        {
            if(points.Count()>0)
                MinBR.GetMBR(points);
        }
        public void updatetextboxes()
        {
            float trans;
            if (Controller.MyMap.Unit == "英寸")
                trans = (float)1 / 96;
            else
                trans = (float)2.54 / 96;
            for (int i = 0; i < texts.Count(); i++)
            {
                Bitmap tx = new Bitmap(Controller.View_Width, Controller.View_Height);
                Graphics a = Graphics.FromImage(tx);
                SizeF textsize = a.MeasureString(texts[i], textfont);
                SizeF realsize = new SizeF(textsize.Width * trans, textsize.Height * trans);
                sizes.RemoveAt(i);
                sizes.Insert(i, realsize);
                SimpleTextBox stb = new SimpleTextBox();
                stb.lupt = new PointF(points[i].X - sizes[i].Width / 2, points[i].Y + sizes[i].Height / 2);
                stb.lbpt = new PointF(points[i].X - sizes[i].Width / 2, points[i].Y - sizes[i].Height / 2);
                stb.rupt  = new PointF(points[i].X + sizes[i].Width / 2, points[i].Y + sizes[i].Height / 2);
                stb.rbpt = new PointF(points[i].X+ sizes[i].Width / 2, points[i].Y - sizes[i].Height / 2);
                textboxes.RemoveAt(i);
                textboxes.Insert(i, stb);
            }
        }
        public void DragCText(Point movepoint)
        {
            for (int i = 0; i < points.Count(); i++)
            {
                Point screen = new Point();
                screen = Transformer.ToScreen(points[i]);
                screen.X = screen.X + movepoint.X;
                screen.Y = screen.Y + movepoint.Y;
                PointF temp = Transformer.FromScreen(screen);
                points.RemoveAt(i);
                points.Insert(i, temp);
            }
            updatetextboxes();
        }
        public void Dragsimpletext(Point movepoint)
        {
            for (int i = 0; i < Controller.selectsimple.Count(); i++)
            {
                Point screen = new Point();
                screen = Transformer.ToScreen(points[Controller.selectsimple[i]]);
                screen.X = screen.X + movepoint.X;
                screen.Y = screen.Y + movepoint.Y;
                PointF temp = Transformer.FromScreen(screen);
                points.RemoveAt(Controller.selectsimple[i]);
                points.Insert(Controller.selectsimple[i], temp);
            }
            updatetextboxes();
        }
        public void Deletesimpletext()
        {
            /*texts.RemoveAt(index);
            points.RemoveAt(index);
            textboxes.RemoveAt(index);
            sizes.RemoveAt(index);,*/
            for (int i = 0; i < Controller.selectsimple.Count(); i++)
            {
                points.RemoveAt(Controller.selectsimple[i]);
                points.Insert(Controller.selectsimple[i], new PointF(-1, -1));
            }
            for (int i = 0; i <points.Count(); i++)
            {
                if (points[i].X == -1)
                {
                    points.RemoveAt(i);
                    texts.RemoveAt(i);
                    textboxes.RemoveAt(i);
                    sizes.RemoveAt(i);
                    i--;
                }
            }          
        }

        public void selectsimpletext(PointF e)
        {
            float trans;
            if (Controller.MyMap.Unit == "英寸")
                trans = (float)1 / 96;
            else
                trans = (float)2.54 / 96;
            float Buffer = 10 * trans;
            for (int j = 0; j < texts.Count(); j++)
            {
                List<PointF> RecBuffer = new List<PointF>();
                RecBuffer.Add(textboxes[j].lbpt);
                RecBuffer.Add(textboxes[j].lupt);
                RecBuffer.Add(textboxes[j].rupt);
                RecBuffer.Add(textboxes[j].rbpt);
                RecBuffer.Add(textboxes[j].lbpt);
                if (Controller.IfPointInPolygon(RecBuffer, new PointF(e.X, e.Y)))
                {
                    for (int m = 0; m < Controller.selectsimple.Count(); m++)
                    {
                        if (j == Controller.selectsimple[m])
                        {
                            Controller.selectsimple.RemoveAt(m);
                            return;
                        }
                    }
                    Controller.selectsimple.Add(j);
                    return;
                }
            }
        }

        /*public void getsizes()
        {
            float trans;
            if (Controller.MyMap.Unit == "英寸")
                trans = (float)1 / 96;
            else
                trans = (float)2.54 / 96;
            Bitmap tx = new Bitmap(Controller.View_Width, Controller.View_Height);
            Graphics a = Graphics.FromImage(tx);
            for (int i = 0; i < texts.Count(); i++)
            {
                SizeF textsize = a.MeasureString(texts[i], textfont);
                SizeF realsize = new SizeF(textsize.Width * trans, textsize.Height * trans);
                sizes.Add(realsize);
            }
        }*/
    }
}
